﻿<?php
function getPagingNav($sql, $pageNum, $rowsPerPage, $queryString = '')
{
	$result  = mysql_query($sql) or die('Error, query failed. ' . mysql_error());
	$row     = mysql_fetch_array($result, MYSQL_ASSOC);
	$numrows = $row['numrows'];
	
	// how many pages we have when using paging?
	$maxPage = ceil($numrows/$rowsPerPage);
	
	$self = $_SERVER['PHP_SELF'];
	
	// creating 'previous' and 'next' link
	// plus 'first page' and 'last page' link
	
	// print 'previous' link only if we're not
	// on page one
	if ($pageNum > 1)
	{
		$page = $pageNum - 1;
		$prev = " <a href=\"$self?page=$page{$queryString}\">[Trở về]</a> ";
	
		$first = " <a href=\"$self?page=1{$queryString}\">[Đầu]</a> "; 
	}
	else
	{
		$prev  = ' [Trở về] ';       // we're on page one, don't enable 'previous' link
		$first = ' [Đầu] '; // nor 'first page' link
	}
	
	// print 'next' link only if we're not
	// on the last page
	if ($pageNum < $maxPage)
	{
		$page = $pageNum + 1;
		$next = " <a href=\"$self?page=$page{$queryString}\">[Tiếp]</a> ";
	
		$last = " <a href=\"$self?page=$maxPage{$queryString}{$queryString}\">[Cuối]</a> ";
	}
	else
	{
		$next = ' [Tiếp] ';      // we're on the last page, don't enable 'next' link
		$last = ' [Cuối] '; // nor 'last page' link
	}
	
	// return the page navigation link
	return $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last; 
}
function chudeList(){
	$sql = "SELECT Chude_id, Chude
			FROM tbl_chude
			ORDER BY Chude_id";
	$result = dbQuery($sql) or die('Cannot get news. ' . mysql_error());
	if(@mysql_num_rows($result)>0){
	while($row = mysql_fetch_array($result)) {
		?><li><a href="index.php?action=tintuc&tt=<?php echo $row['Chude_id'];?>"><?php echo $row['Chude']; ?></a></li>			
	<?php	}	
} else echo "<h3 id=h3tt>Chưa có chủ đề</h3>";}
/////////////////////////////////////////////////////////////////////////
function lienquanList(){
	$sql = "SELECT Chude_id, Chude
			FROM tbl_chude
			ORDER BY Chude_id";
	$result = dbQuery($sql) or die('Cannot get news. ' . mysql_error());
	if(@mysql_num_rows($result)>0){
	while($row = mysql_fetch_array($result)) {
		?><li><a href="index.php?action=tintuc&tt=<?php echo $row['Chude_id'];?>"><?php echo $row['Chude']; ?></a></li>			
	<?php	}	
} echo "<h3 id=h3tt>Chưa có chủ đề</h3>";}

function productMenuList(){
	$sql = "SELECT cat_id, cat_name
			FROM tbl_category
			WHERE cat_parent_id='0'
			ORDER BY cat_id";
	$result = dbQuery($sql) or die('Cannot get news. ' . mysql_error());
	if(@mysql_num_rows($result)>0){
	while($row = mysql_fetch_array($result)) {
		?><li><a href="index.php?c=<?php echo $row['cat_id'];?>"><?php echo $row['cat_name']; ?></a></li>			
	<?php	}	
}}

function buildCategoryOptions($catId = 0)
{
	$sql = "SELECT cat_id, cat_parent_id, cat_name
			FROM tbl_category
			ORDER BY cat_id";
	$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());
	
	$categories = array();
	while($row = dbFetchArray($result)) {
		list($id, $parentId, $name) = $row;
		
		if ($parentId == 0) {
			// we create a new array for each top level categories
			$categories[$id] = array('name' => $name, 'children' => array());
		} else {
			// the child categories are put int the parent category's array
			$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);	
		}
	}	
	
	// build combo box options
	$list = '';
	foreach ($categories as $key => $value) {
		$name     = $value['name'];
		$children = $value['children'];
		
		$list .= "<optgroup label=\"$name\">"; 
		
		foreach ($children as $child) {
			$list .= "<option value=\"{$child['id']}\"";
			if ($child['id'] == $catId) {
				$list.= " selected";
			}
			
			$list .= ">{$child['name']}</option>\r\n";
		}
		
		$list .= "</optgroup>";
	}
	
	return $list;
}
?>